package com.xiaodou.mjf.entity.basic;

import com.google.common.collect.Lists;
import com.xiaodou.mjf.entity.basic.base.OperatorBase;
import com.xiaodou.mjf.infrastructure.common.utils.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import java.io.Serializable;
import java.util.List;

/**
 * Operator实体定义
 *
 * @author XiaoDou.com Code Generator
 * @version 1.0
 *          2015-02-28 14:33:19
 */
@Entity
@DynamicInsert
@DynamicUpdate
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Operator extends OperatorBase implements Serializable {

    /**
     * 序列化标识
     */
    private static final long serialVersionUID = -3709116567695504131L;

    @Transient
    private OperatorProfile profile;

    @OneToMany
    @JoinTable(name = "operator_role",
            joinColumns = {@JoinColumn(name = "operator_id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id")})
    private List<Role> roles = Lists.newArrayList();

    @Transient
    private String roleIds;

    @Transient
    private List<String> roleIdList;

    public Operator() {
    }

    public Operator(String id) {
        this.id = id;
    }

    public OperatorProfile getProfile() {
        return profile;
    }

    public void setProfile(OperatorProfile profile) {
        this.profile = profile;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
        if(null == roles || roles.size()==0){
            this.roleIds = "";
            this.roleIdList = Lists.newArrayList();
        } else {
            boolean first = true;
            StringBuilder sbRoleIds = new StringBuilder();
            this.roleIdList = Lists.newArrayList();
            for (Role role : roles) {
                if(!first) {
                    sbRoleIds.append(",");
                }
                first = false;
                sbRoleIds.append(role.getId());
                roleIdList.add(role.getId());
            }
            this.roleIds = sbRoleIds.toString();
        }
    }

    public boolean isSuper() {
        if (this.roles.size() == 0) {
            return false;
        } else {
            for (Role role : roles) {
                if (role.getCode().equalsIgnoreCase("SUPER")) {
                    return true;
                }
            }
        }
        return false;
    }

    public String getRoleIds() {
        List<String> nameIdList = Lists.newArrayList();
        for (Role role : roles) {
            nameIdList.add(role.getId());
        }
        return StringUtils.join(nameIdList, ",");
    }

    public void setRoleIds(String roleIds) {
        roles = Lists.newArrayList();
        if (roleIds != null) {
            String[] ids = StringUtils.split(roleIds, ",");
            for (String roleId : ids) {
                Role role = new Role(roleId);
                roles.add(role);
            }
        }
    }

    public List<String> getRoleIdList() {
        List<String> roleIdList = Lists.newArrayList();
        for (Role role : roles) {
            roleIdList.add(role.getId());
        }
        return roleIdList;
    }

    public void setRoleIdList(List<String> roleIdList) {
        roles = Lists.newArrayList();
        for (String roleId : roleIdList) {
            Role role = new Role();
            role.setId(roleId);
            roles.add(role);
        }
    }
}
